package ro.msg.quiz.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

//AUTHOR: Ervin

//View that represents the main window
@SuppressWarnings("serial")
public class SmartQuest extends JFrame {

    private static final int frameHeight = 400;
    private static final int frameWidth = 400;

    private JPanel toolbarPanel = new JPanel();
    private JTabbedPane tabs = new JTabbedPane();

    private JButton add = new JButton("Add");
    private JButton edit = new JButton("Edit");
    private JButton delete = new JButton("Delete");
    private JButton importt = new JButton("Import");
    private JButton export = new JButton("Export");
    private JButton show = new JButton("Show");

    private JTable questionairesTable;
    private JTable questionTable;
    private DefaultTableModel questionairesModel;
    private DefaultTableModel questionModel;
    private JScrollPane scrollQuestionaires;
    private JScrollPane scrollQuestions;

    private JPanel questionPanel = new JPanel();

    public SmartQuest(String[][] data1, String[][] data2) {
	this.setSize(new Dimension(frameWidth, frameHeight));
	this.setTitle("SmartQuest 1.0");

	// ///////////////////toolbar/////////////////////
	toolbarPanel.add(add);
	toolbarPanel.add(edit);
	toolbarPanel.add(delete);
	toolbarPanel.add(importt);
	toolbarPanel.add(export);
	this.add(toolbarPanel, BorderLayout.NORTH);
	// ///////////////////////////////////////////////

	// ////////////////tables////////////////////////
	questionairesModel = new DefaultTableModel();
	questionModel = new DefaultTableModel();
	this.questionariesInit(data1);
	this.questionInit(data2);
	tabs.addTab("Questionaires", scrollQuestionaires);
	tabs.addTab("Questions", questionPanel);
	this.add(tabs);
	// //////////////////////////////////////////////
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setVisible(true);
    }

    public void questionariesInit(String[][] data) {
	String[] column = { "Name", "Number of quesitons", "Last edited" };

	questionairesModel.setDataVector(data, column);
	questionairesTable = new JTable(questionairesModel) {
	    @Override
	    public boolean isCellEditable(int row, int clmn) {
		return false;
	    }
	};
	questionairesTable.setPreferredScrollableViewportSize(new Dimension(400, 200));
	questionairesTable.setFillsViewportHeight(true);
	questionairesTable.setRowHeight(50);

	scrollQuestionaires = new JScrollPane(questionairesTable);

    }

    public void questionInit(String[][] data) {
	String[] column = { "Question", "Type", "Last edited" };

	questionModel.setDataVector(data, column);
	questionTable = new JTable(questionModel) {
	    @Override
	    public boolean isCellEditable(int row, int clmn) {
		return false;
	    }

	};
	questionTable.setPreferredScrollableViewportSize(new Dimension(400, 200));
	questionTable.setFillsViewportHeight(true);
	questionTable.setRowHeight(50);
	questionPanel = new JPanel();
	scrollQuestions = new JScrollPane(questionTable);
	questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.Y_AXIS));
	questionPanel.add(scrollQuestions);
	questionPanel.add(show);
    }

    /**
     * @return the add
     */
    public JButton getAdd() {
	return add;
    }

    /**
     * @return the edit
     */
    public JButton getEdit() {
	return edit;
    }

    /**
     * @return the delete
     */
    public JButton getDelete() {
	return delete;
    }

    /**
     * @return the importt
     */
    public JButton getImportt() {
	return importt;
    }

    /**
     * @return the export
     */
    public JButton getExport() {
	return export;
    }

    public JTabbedPane getTabs() {
	return tabs;
    }

    public void deleteRow() {
	if (tabs.getSelectedIndex() == 0) {
	    int[] selection = questionairesTable.getSelectedRows();
	    for (int i = 0; i < selection.length; i++)
		questionairesModel.removeRow(selection[i] - i);
	} else {
	    int[] selection = questionTable.getSelectedRows();
	    for (int i = 0; i < selection.length; i++)
		questionModel.removeRow(selection[i] - i);
	}
    }

    public String[] getSelectedRows() {
	if (tabs.getSelectedIndex() == 0) {
	    int[] selection = questionairesTable.getSelectedRows();
	    String[] s = new String[selection.length];
	    for (int i = 0; i < selection.length; i++)
		s[i] = (String) questionairesModel.getValueAt(selection[i], 0);
	    return s;
	} else {
	    int[] selection = questionTable.getSelectedRows();
	    String[] s = new String[selection.length];
	    for (int i = 0; i < selection.length; i++)
		s[i] = (String) questionModel.getValueAt(selection[i], 0);
	    return s;
	}
    }

    public String[] getEditInfo() {
	String s[] = new String[5];
	int rowSelected;

	if (tabs.getSelectedIndex() == 0) {
	    rowSelected = questionairesTable.getSelectedRow();
	    s[0] = (String) questionairesModel.getValueAt(rowSelected, 0);
	    s[1] = (String) questionairesModel.getValueAt(rowSelected, 1);
	    s[2] = (String) questionairesModel.getValueAt(rowSelected, 2);
	    s[3] = "Quiz";
	    s[4] = String.valueOf(rowSelected);
	} else {
	    rowSelected = questionTable.getSelectedRow();
	    s[0] = (String) questionModel.getValueAt(rowSelected, 0);
	    s[1] = (String) questionModel.getValueAt(rowSelected, 1);
	    s[2] = (String) questionModel.getValueAt(rowSelected, 2);
	    s[3] = "Question";
	    s[4] = String.valueOf(rowSelected);
	}

	return s;
    }

    public void updateTable(int row, String[] data, String tableName) {
	if (tableName.equals("Quiz")){
	    for(int i=0;i<data.length-1;i++)
		questionairesModel.setValueAt(data[i], row, i);
	}
	else
	    for(int i=0;i<data.length-1;i++)
	    questionModel.setValueAt(data[i], row, i);
    }

    public String getSelectedItem() {
	if (tabs.getSelectedIndex() == 0)
	    return questionairesTable.getValueAt(questionairesTable.getSelectedRow(), 0).toString();
	else
	    return questionTable.getValueAt(questionTable.getSelectedRow(), 0).toString();
    }

    public void addQuizInTable(String name, int nrOfQuestions, String string) {
	String rowData[] = new String[3];
	rowData[0] = name;
	rowData[1] = String.valueOf(nrOfQuestions);
	rowData[2] = string;
	questionairesModel.addRow(rowData);
    }

    public void addQuestionInTable(String text, String type, String string) {
	String rowData[] = new String[3];
	rowData[0] = text;
	rowData[1] = type;
	rowData[2] = string;
	questionModel.addRow(rowData);
    }

}
